package com.qs.javame.thread.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * 测试4：执行join()操作时，线程被中断，抛出InterruptedException异常，标识位会被清除，isInterrupted()返回false
 */
public class TestInterrupt5 {
    public static void main(String[] args) {
        Thread thread = new Thread(new Task());
        thread.start();

        //这里的tread.join()会抛出InterruptedException异常，需要中断main线程
        Thread.currentThread().interrupt();

        //这里中断thread线程，该线程会在sleep()时被中断，抛出InterruptedException异常
        thread.interrupt();

        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println(Thread.currentThread() + "-线程中断1：" + Thread.currentThread().isInterrupted());
        }

        System.out.println("main线程执行完了~");
    }

    static class Task implements Runnable {

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(2);

                System.out.println(Thread.currentThread().getName() + "执行结束~");
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(Thread.currentThread() + "-线程中断2：" + Thread.currentThread().isInterrupted());
            }
        }
    }
}


